package inglar.autotc.client.ui;

import inglar.autotc.client.Utilities;
import inglar.autotc.client.resource.ResourceHandler;
import inglar.autotc.shared.Values;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PageClients extends Composite {
	private HorizontalPanel contentPanel;
	private VerticalPanel clientTablePanel;
	private VerticalPanel netstatPanel;
	private FlexTable clientList;

	public PageClients() {
		
		contentPanel = new HorizontalPanel();
		initWidget(contentPanel);
		
		clientTablePanel = new VerticalPanel();
		
		netstatPanel = new VerticalPanel();
		netstatPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		
		contentPanel.add(new Image(ResourceHandler.INSTANCE.fbLoadBlue()));
		
		clientList = new FlexTable();
		clientList.setCellPadding(5);
//		userlist.setBorderWidth(1);
		
		String userid = Cookies.getCookie("QID");
		
		AsyncCallback<String[]> getClientListCallback = new AsyncCallback<String[]>() {
			@Override
			public void onSuccess(String[] result) {
				loadClientList(result);
			}
			@Override
			public void onFailure(Throwable caught) {
				Utilities.failureAlert("getClientListCallback", "RPC failure", caught);
			}
		};
		
		Utilities.rpc.getClientList(userid, getClientListCallback);
		
	}

	protected void loadClientList(String[] result) {
		contentPanel.clear();
		
		contentPanel.add(clientTablePanel);
		contentPanel.add(netstatPanel);
		
		clientTablePanel.add(clientList);
		
		HTML htmlIpAddress = new HTML("<b>IP Address</b>");
		clientList.setWidget(0, 0, htmlIpAddress);
		htmlIpAddress.getElement().setAttribute("style", "margin-right: 40px;");
		
		HTML htmlPort = new HTML("<b>Client Port</b>");
		clientList.setWidget(0, 1, htmlPort);
		htmlPort.getElement().setAttribute("style", "margin-right: 40px;");
		
		if(result.length == 0) {
			clientList.setWidget(1, 0, new HTML("<b style='color: red'>No known clients.</b>"));
		} else {
			for(int i = 0; i < result.length; i++) {
				String[] t = result[i].split(Values.REGEX_ALL_WHITESPACE);
				final String ipaddr = t[0].trim();
				final String port = t[1].trim();
				clientList.setWidget(i + 1, 0, new HTML(ipaddr));
				clientList.setWidget(i + 1, 1, new HTML(port));
				
				final Button btnDelRow = new Button("<b>x</b>");
				clientList.setWidget(i + 1, 2, btnDelRow);
				btnDelRow.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						
						Utilities.showLoadingModal();
						
						AsyncCallback<String> delClientCallback = new AsyncCallback<String>() {
							@Override
							public void onSuccess(String result) {
								
								Utilities.hideLoadingModal();
								
								if(result.trim().isEmpty()) {
									Window.alert("Successfully deleted client " + ipaddr + ":" + port);
									int numRows = clientList.getRowCount();
									for(int i = 1; i < numRows; i++) {
										if(clientList.getWidget(i, 2) == btnDelRow) {
											clientList.removeRow(i);
											break;
										}
									}
								} else {
									Window.alert(result.trim());
									Location.reload();
								}
							}
							@Override
							public void onFailure(Throwable caught) {
								Utilities.failureAlert("delClientCallback", "RPC failure", caught);
								Location.reload();
							}
						};
						
						String userid = Cookies.getCookie("QID");
						Utilities.rpc.deleteClient(userid, ipaddr, new Integer(port), delClientCallback);
					}
				});
				
				final Button btnNetstat = new Button("netstat");
				clientList.setWidget(i + 1, 3, btnNetstat);
				btnNetstat.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						netStat(ipaddr, port);
					}
				});
			}
		}
		
		Button btnAddRow = new Button("Add Client");
		clientTablePanel.add(btnAddRow);
		btnAddRow.getElement().setAttribute("style", "margin-top: 3px; margin-bottom: 3px;");
		btnAddRow.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				DialogBoxAddClient acdb = new DialogBoxAddClient();
				acdb.center();
			}
		});
	}

	protected void netStat(final String ipaddress, final String port) {
		netstatPanel.clear();
		
		AsyncCallback<String[]> netstatCallback = new AsyncCallback<String[]>() {
			@Override
			public void onSuccess(String[] result) {
				netstatPanel.clear();
				String html = "";
				for(int i = 0; i < result.length; i++) {
					html += "<code>" + Utilities.whiteSpace(result[i]) + "</code><br>";
				}
				HTML h = new HTML("showing netstat results for " + ipaddress + ":" +port + "<br>\n" + html);
				netstatPanel.add(h);
//				h.getElement().setAttribute("style", "margin-left: 7px;");
			}
			@Override
			public void onFailure(Throwable caught) {
				Utilities.failureAlert("netstatCallback", "RPC failure", caught);
			}
		};
		
		String userid = Cookies.getCookie("QID");
		Utilities.rpc.netstat(userid, ipaddress, new Integer(port), netstatCallback);
	}

}
